import torch
import SettingUp as  s
import BuildingTheNetWorking as B
import torch.nn.functional as F

train_losses = []
train_counter = []
test_losses = []
test_counter = [i*len(s.train_loader.dataset) for i in range(s.n_epochs + 1)]


def train(epoch):
    B.network.train()
    for batch_idx, (data, target) in enumerate(s.train_loader):
        B.optimizer.zero_grad()
        output = B.network(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        B.optimizer.step()
        if batch_idx % s.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(s.train_loader.dataset),
                       100. * batch_idx / len(s.train_loader), loss.item()))
            train_losses.append(loss.item())
            train_counter.append(
                (batch_idx * 64) + ((epoch - 1) * len(s.train_loader.dataset)))
            torch.save(B.network.state_dict(), './model.pth')
            torch.save(B.optimizer.state_dict(), './optimizer.pth')
